<?php
	$strSearch = '<span class="title">Danh sách khách hàng</span>';
	$strSearchRight = '';
?>

<div class="left_content">
	<?php echo $this->render($this->arrParam['controller'] . '/toolbar/left.php');?>
</div>

<div class="right_content">
	<div class="appContent">
		<form action="" method="post" name="appForm">
			<?php echo $this->render($this->arrParam['controller'] . '/toolbar/index.php');?>
			<div id="tabs">
				<ul>
					<li><a href="#tabs-1">Danh sách khách hàng</a></li>
					<li><a href="#tabs-2">Nội dung email</a></li>
				</ul>
				<div id="tabs-1">
					<div class="tblForm">
						<div class="tbl_content">
							<?php
							if(!empty($this->arrParam['id'])) {
								echo '<input type="hidden" name="email_type" id="email_type" value="'.$this->arrParam['id'].'">';
							} else {
								echo '<input type="hidden" name="email_type" id="email_type" value="0">';
							}
							?>
							<table class="tblList" id="tblListEmail">
								<tr>
									<th width="70">#</th>
									<th width="200">Email</th>
									<th>Tên khách hàng</th>
									<th>Điện thoại</th>
									<th width="100">Trạng thái</th>
								</tr>
								<?php
								$i = 0;
								foreach ($this->Khachhang as $key => $val){
									$i++; 
									$id 			= $val['id'];
									$name 			= $val['name'];
									$phone 			= $val['phone'];
									$email 			= $val['email'];
								
									if(!empty($val['email'])) {
									$classEmail = 'item_email';
									$khachhang_trangthai = '';
									if($val['trangthai_email'] == 1) {
										$classEmail = '';
										$khachhang_trangthai = '<span class="red">Đã gửi</span>';
									}
								?>
								<tr class="<?php echo $classEmail;?>">
									<td align="center">
										<?php echo $i;?>
										<input type="hidden" id="khachhang_id" value="<?php echo $id;?>">
									</td>
									<td align="left" id="khachhang_email"><?php echo $email;?></td>
									<td align="left" id="khachhang_name"><?php echo $name;?></td>
									<td align="center" id="khachhang_phone"><?php echo $phone;?></td>
									<td align="center" id="khachhang_trangthai"><?php echo $khachhang_trangthai;?></td>
								</tr>
								<?php
									}
								} 
								?>
							</table>
						</div>
					</div>
				</div>
				<div id="tabs-2">
					<?php
					$title 		= $this->formText ( 'title', $this->Marketing['title'], array ('class' => 'txtlong') );
					$content 	= $this->cmsEditor('content',htmlspecialchars_decode($this->Marketing['noidungemail']),array('toolbar'=>'MyToolbar', 'language'=>'vi', 'skin' => 'v2'), 'auto', 200, true, 500, false);
					$file		= '<img src="" id="image_file" style="width:0;height:0;">';
					$file	 	= $this->cmsUserFiles('file', $this->Marketing['file'], 'Chọn File', 'file', $options = array('class' => 'txtfile') ) . $file;
					?>
					<div class="tblForm">
						<div class="tbl_content">
							<table class="tbl">
								<tr>
									<td class="label">Tiêu đề <span class="red">*</span></td>
									<td><?php echo $title;?></td>
								</tr>
								<tr>
									<td class="label">Nội dung <span class="red">*</span></td>
									<td><?php echo $content;?></td>
								</tr>
								<tr>
									<td class="label">File đính kèm</td>
									<td><?php echo $file;?></td>
								</tr>
							</table>
						</div>
					</div>
				</div>
			</div>
		</form>
	</div>
	<?php
	$siteConfig = Zend_Registry::get('siteConfig');
	?>
	<script type="text/javascript">
		function sendEmailAll() {
			var tongTr = $('#tblListEmail tr').size();
			var tongEmail = $('#tblListEmail .item_email').size();
			if(tongTr > 1) {
				if(tongEmail == 0) {
					if(confirm('Danh sách này đã được gửi. Bạn có muốn gửi lại 1 lần nữa?')) {
						if($('#title').val() == '') {
							alert('Bạn chưa nhập tiêu đề mail');
							$('#title').focus();
							return false;
						}
						if(confirm('Bắt đầu gửi')) {
							$('#tblListEmail tr').each(function(index){
								if(index > 0){
									$(this).addClass('item_email');
									$('#khachhang_trangthai', this).text('');
								}
							});
							
							tongEmail = $('#tblListEmail .item_email').size();
							var i = 0;
							$('#tblListEmail .item_email #khachhang_trangthai').addClass('dangtai2');
							intervalId = setInterval(function(){
								$.ajax({
									url: '<?php echo $this->baseUrl('marketing/ajax/gui-email');?>',
									type: 'POST',
									dataType: 'html',
									data: { 
										id			: $('#tblListEmail .item_email:first #khachhang_id').val(), 
										file_name	: $('#email_type').val(), 
										email		: $('#tblListEmail .item_email:first #khachhang_email').text(), 
										name		: $('#tblListEmail .item_email:first #khachhang_name').text(),
										title		: $('#title').val(),
										content		: CKEDITOR.instances['content'].getData(),
										email_type	: $('#email_type').val()
									},
									beforeSend: function(){},
								 	success: function(html){
								 		$('#tblListEmail .item_email:first #khachhang_trangthai').html('<span class="red">Gửi xong</span>');
								 		//$('#tblListEmail .item_email:first #khachhang_trangthai').html(html);
								 		$('#tblListEmail .item_email:first #khachhang_trangthai').removeClass('dangtai2');
								 		$('#tblListEmail .item_email:first').removeClass('item_email');
									}
								});
								
								i++;
								if(i == tongEmail) {
									clearInterval(intervalId);
									alert('Đã gửi xong');
								}
							}, <?php echo $siteConfig['config_mail']['time'] * 1000;?>);
						}
					}
				} else {
					if($('#title').val() == '') {
						alert('Bạn chưa nhập tiêu đề mail');
						$('#title').focus();
						return false;
					}
					if(confirm('Bắt đầu gửi')) {
						var i = 0;
						$('#tblListEmail .item_email #khachhang_trangthai').addClass('dangtai2');
						intervalId = setInterval(function(){
							$.ajax({
								url: '<?php echo $this->baseUrl('marketing/ajax/gui-email');?>',
								type: 'POST',
								dataType: 'html',
								data: { 
									id			: $('#tblListEmail .item_email:first #khachhang_id').val(), 
									file_name	: $('#email_type').val(), 
									email		: $('#tblListEmail .item_email:first #khachhang_email').text(), 
									name		: $('#tblListEmail .item_email:first #khachhang_name').text(),
									title		: $('#title').val(),
									content		: CKEDITOR.instances['content'].getData(),
									email_type	: $('#email_type').val()
								},
								beforeSend: function(){},
							 	success: function(html){
							 		$('#tblListEmail .item_email:first #khachhang_trangthai').html('<span class="red">Gửi xong</span>');
							 		//$('#tblListEmail .item_email:first #khachhang_trangthai').html(html);
							 		$('#tblListEmail .item_email:first #khachhang_trangthai').removeClass('dangtai2');
							 		$('#tblListEmail .item_email:first').removeClass('item_email');
								}
							});
							
							i++;
							if(i == tongEmail) {
								clearInterval(intervalId);
								alert('Đã gửi xong');
							}
						}, <?php echo $siteConfig['config_mail']['time'] * 1000;?>);
					}
				}
			} else {
				alert('Không có danh sách email để gửi');
			}
		}

	</script>
</div>
<div class="clr"></div>	

